/*************************************************
Author: will
Date: 11/8/2012
Description:
Notes: 
*************************************************/

#ifndef __CCardMgr_h__
#define __CCardMgr_h__

#include "CCardConfig.h"
#include "CUserCard.h"
#include "MsgCard.pb.h"
#include "../storage/CUserStorage.h"

namespace CTLib
{
    class CEnpNetHead;
};

class MsgCardDetailInfo;
class CPlayerBaseInfo;
class CPlayerDetailInfo;
class MsgAnsItemBaseInfos;
class PBMsgHead;
class MsgAnsItemDetailInfos;
class CCardMgr
{
public:
    CCardMgr()
    {

    }

    virtual ~CCardMgr()
    {

    }

    friend class CAuctionMgr;

    int LoadConfig(const char* szCfgPath);

    //翻牌
    int TurnOverCard(CPlayerBaseInfo& stPlayerBaseInfo, CPlayerDetailInfo& stPlayerDetailInfo, MsgAnsTurnOver& stMsgAnsTurnOver);

    //金币占卜
    int GoldTurnOverCard(CPlayerBaseInfo& stPlayerBaseInfo, CPlayerDetailInfo& stPlayerDetailInfo, int iMaxCanDivineCount, MsgAnsTurnOver& stMsgAnsTurnOver);

    //收取金币占卜的卡片
    int GoldDropCard(CUserCard& stUserCard, CPlayerBaseInfo& stPlayerBaseInfo, CPlayerDetailInfo& stPlayerDetailInfo, MsgAnsTurnOver& stMsgAnsTurnOver);

    //获取金币占卜次数
    int GetGoldTurnOverCount(unsigned char ucVipLevel, int iHasOptCount);

    //初始化数据
    int InitData(CUserCard& stUserCard, unsigned int uiGID, CUserCardData& stUserCardData);

    int AddCard(CUserCard& stUserCard, CSingleCardData& stSingleCardData);

    int GetData(CUserCard& stUserCard, CUserCardData& stUserCardData);

    //获取卡片信息
    int GetCardInfo(CUserCard& stUserCard, unsigned int uiCardID, CSingleCardData& stCardData);
    int GetCardInfo(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo, unsigned int uiCardID, MsgAnsItemBaseInfos& stCardDetailInfo);
    int GetCardInfo(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo, unsigned int uiCardID, MsgGridItemInfo& stCardBaseInfo);
    int GetCardInfo(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo, unsigned int uiCardID, MsgCardDetailInfo& stCardDetailInfo);
    
    //获取穿戴的卡片属性加成
    int GetCardAttr(CUserCard& stUserCard, int iRoleID, CPlayerDetailInfo& stPlayerDetailInfo, CPlayerAttr& stPlayerAttr);

    //获取穿戴卡片的属性加成
    int GetTotalLevel(CUserCard& stUserCard, CPlayerWareCard& stPlayerWareCard, int& iTotalLevel);

    int GetCardInfoByPos(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo, unsigned short usBagPos, MsgGridItemInfo& stBaseCardInfo);
    int GetCardInfoByPos(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo, unsigned short usBagPos, MsgCardDetailInfo& stCardDetailInfo);

    //收取卡片
    int DropCard(CUserCard& stUserCard, CPlayerBaseInfo& stPlayerBaseInfo, 
                        CPlayerDetailInfo& stPlayerDetailInfo, bool bIsMerge);

    //掉落指定的卡片
    int DropCard(CUserCard& stUserCard, unsigned short usCardConfigID, int iNowExp, bool bBroadCastMsg = true);

    //一键合成卡片背包
    int MergeCardBag(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo);

    //是否是合法的穿卡片位置
    bool IsValidWareCardPos(unsigned char ucPos, unsigned short usRoleLevel)
    {
        if (usRoleLevel < 30)
        {
            return ucPos <= 1;
        }
        else if (usRoleLevel < 40)
        {
            return ucPos <= 2;
        }
        else if (usRoleLevel < 50)
        {
            return ucPos <= 3;
        }
        else if (usRoleLevel < 60)
        {
            return ucPos <= 4;
        }
        else if (usRoleLevel < 70)
        {
            return ucPos <= 5;
        }
        else if (usRoleLevel < 80)
        {
            return ucPos <= 6;
        }
        else if (usRoleLevel >= 80)
        {
            return ucPos <= 7;
        }

        return false;
    }

    //穿卡片
    int WareCard(CUserCard& stUserCard, CPlayerWareCard& stPlayerWareCard, 
        int iRoleID, unsigned short usRoleLevel, unsigned short usCardBagPos, unsigned char ucRolePos, CPlayerDetailInfo& stPlayerDetailInfo);

    //身上的卡片位置移动
    int MoveWareCard(CUserCard& stUserCard, CPlayerWareCard& stPlayerWareCard, int iRoleID, 
                    unsigned short usRoleLevel, unsigned char ucFromPos, unsigned char ucToPos);

    //脱卡片
    int TakeOffCard(CUserCard& stUserCard, CPlayerWareCard& stPlayerWareCard, 
        unsigned char ucRolePos, unsigned short& usCardBagPos);

    //获取下一级命运漩涡配置
    int GetVortexConfig(unsigned short usLevel, CVortexConfigData& stConfigData);

    //命运漩涡升级
    int VortexLevelUp(int& iNowFateForce, CCardVortex& stCardVortex);

    //命运漩涡降级
    int VortexLevelDown(int& iNowFateForce, CPlayerBaseInfo& stPlayerBaseInfo, CCardVortex& stCardVortex, bool bDownAll);

    //卡片合成
    int CardCombine(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo, 
                        unsigned int uiFromCardID, unsigned int uiToCardID, bool bDestIsTo, unsigned int& uiDestCardID);

    //卡片交换位置
    int SwapStorageItemPos(CUserCard& stUserCard, int iItemPos1,CStorageGrid & stGrid1, int iItemPos2,CStorageGrid & stGrid2);

    //兑换卡片
    int ExchangeCard(CUserCard& stUserCard, unsigned short usCardConfigID, unsigned short usRoleLevel, int& iRomany, CPlayerDetailInfo& stPlayerDetailInfo);

    void GetCardDetailInfo(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo, 
        const ::google::protobuf::RepeatedField<int64>& stReqIDList, MsgAnsItemDetailInfos& stAns);

    //获取卡片分数
    int GetCardPoint(CUserCard& stUserCard, unsigned int uiCardID, unsigned int& uiPoint);
    int GetCardPoint(CUserCard& stUserCard, CPlayerWareCard& stPlayerWareCard, unsigned int& uiPoint);

    //发送占卜信息
    void SendDivineInfoToFront(CUserCard& stUserCard, CPlayerBaseInfo& stPlayerBaseInfo, 
        CUserDivineData& stUserDivineData, PBMsgHead stMsgHead, CTLib::CEnpNetHead& stEnpHead);

    //发送卡片背包信息
    void SendCardBagInfoToFront(CUserCard& stUserCard, PBMsgHead stMsgHead, CTLib::CEnpNetHead& stEnpHead);

    //发送卡片背包详情
    void SendCardDetailInfoToFront(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo, const ::google::protobuf::RepeatedField<int64>& stReqIDList, 
                                                PBMsgHead stMsgHead, CEnpNetHead stEnpHead);

    //发送主角身上的卡片
    void SendCardOnRoleToFront(CUserCard& stUserCard, CPlayerBaseInfo& stPlayerBaseInfo, CPlayerDetailInfo& stPlayerDetailInfo, PBMsgHead stMsgHead, CEnpNetHead stEnpHead);

    //获取穿着的卡片信息
    void GetWareCardDetialInfo(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo, CPlayerWareCard& stPlayerWareCard, 
                    ::google::protobuf::RepeatedPtrField<MsgCardDetailInfo>& stDetailInfo);

    //打印信息
    void DumpCardInfo(CUserCard& stUserCard);

    int GetCardConfig(unsigned short usCardConfigID, CCardConfigData& stConfigData)
    {
        return _stCardConfig.GetConfig(usCardConfigID, stConfigData);
    }

private:
    //用于从占卜向背包掉落，usRoleLevel是用户当前级别
    int DropCard(CUserCard& stUserCard, unsigned char ucCardType, unsigned short usRoleLevel, int iNowExp);

    //抽取卡片经验， 只抽取蓝色及以下卡片
    int ExtractCardTotalExp(CUserDivineData& stUserDivineData, int& iTotalExp);

    int AddToBag(CUserCard& stUserCard, CSingleCardData& stSingleCardData);

    int GetPosValue(unsigned char ucCardType, unsigned short usCardLevel, unsigned short usCardConfigID);
    int GetCardLevel(unsigned char ucCardType, unsigned short usFromLevel, unsigned int uiNowExp, unsigned short& usNewLevel);

    //计算合并优先权值
    int GetMergeValue(unsigned char ucCardType, unsigned short usCardLevel);

    //脱下卡片，但不放入任何地方，需要在外部处理
    int TakeOffCard(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo, 
                            int iRoleID, unsigned char ucRolePos, unsigned int uiCardID);

    //穿上卡片
    int WareCard(CUserCard& stUserCard, CPlayerDetailInfo& stPlayerDetailInfo, 
        int iRoleID, unsigned char ucRolePos, unsigned int uiCardID);

    int GetVortexLevel(int iFateForce, unsigned short& usLevel);

private:
    CDivinePosConfig _stDivinePosConfig;
    CCardLevelConfig _stCardLevelConfig;
    CCardConfig      _stCardConfig;
    CVortexConfig    _stVortexConfig;
};


#endif // __CCardMgr_h__

